{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use Close-Form Policy to Play Acrobot-v1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import logging\n",
    "import imp\n",
    "import itertools\n",
    "\n",
    "import numpy as np\n",
    "np.random.seed(0)\n",
    "import gym\n",
    "\n",
    "imp.reload(logging)\n",
    "logging.basicConfig(level=logging.DEBUG,\n",
    "        format='%(asctime)s [%(levelname)s] %(message)s',\n",
    "        stream=sys.stdout, datefmt='%H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11:21:39 [INFO] env: <AcrobotEnv<Acrobot-v1>>\n",
      "11:21:39 [INFO] action_space: Discrete(3)\n",
      "11:21:39 [INFO] observation_space: Box(6,)\n",
      "11:21:39 [INFO] reward_range: (-inf, inf)\n",
      "11:21:39 [INFO] metadata: {'render.modes': ['human', 'rgb_array'], 'video.frames_per_second': 15}\n",
      "11:21:39 [INFO] _max_episode_steps: 500\n",
      "11:21:39 [INFO] _elapsed_steps: None\n"
     ]
    }
   ],
   "source": [
    "env = gym.make('Acrobot-v1')\n",
    "env.seed(0)\n",
    "for key in vars(env):\n",
    "    logging.info('%s: %s', key, vars(env)[key])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class CloseFormAgent:\n",
    "    def __init__(self, _):\n",
    "        pass\n",
    "\n",
    "    def reset(self, mode=None):\n",
    "        pass\n",
    "\n",
    "    def step(self, observation, _reward, _done):\n",
    "        x0, y0, x1, y1, v0, v1 = observation\n",
    "        if v1 < -0.3:\n",
    "            action = 0\n",
    "        elif v1 > 0.3:\n",
    "            action = 2\n",
    "        else:\n",
    "            y = y1 + x0 * y1 + x1 * y0\n",
    "            if y > 0.:\n",
    "                action = 0\n",
    "            else:\n",
    "                action = 2\n",
    "        return action\n",
    "\n",
    "    def close(self):\n",
    "        pass\n",
    "\n",
    "\n",
    "agent = CloseFormAgent(env)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11:21:39 [INFO] ==== test ====\n",
      "11:21:39 [DEBUG] test episode 0: reward = -70.00, steps = 71\n",
      "11:21:39 [DEBUG] test episode 1: reward = -194.00, steps = 195\n",
      "11:21:39 [DEBUG] test episode 2: reward = -71.00, steps = 72\n",
      "11:21:39 [DEBUG] test episode 3: reward = -78.00, steps = 79\n",
      "11:21:39 [DEBUG] test episode 4: reward = -64.00, steps = 65\n",
      "11:21:39 [DEBUG] test episode 5: reward = -157.00, steps = 158\n",
      "11:21:39 [DEBUG] test episode 6: reward = -71.00, steps = 72\n",
      "11:21:39 [DEBUG] test episode 7: reward = -74.00, steps = 75\n",
      "11:21:39 [DEBUG] test episode 8: reward = -91.00, steps = 92\n",
      "11:21:39 [DEBUG] test episode 9: reward = -138.00, steps = 139\n",
      "11:21:39 [DEBUG] test episode 10: reward = -92.00, steps = 93\n",
      "11:21:39 [DEBUG] test episode 11: reward = -90.00, steps = 91\n",
      "11:21:39 [DEBUG] test episode 12: reward = -107.00, steps = 108\n",
      "11:21:39 [DEBUG] test episode 13: reward = -85.00, steps = 86\n",
      "11:21:39 [DEBUG] test episode 14: reward = -78.00, steps = 79\n",
      "11:21:39 [DEBUG] test episode 15: reward = -85.00, steps = 86\n",
      "11:21:39 [DEBUG] test episode 16: reward = -64.00, steps = 65\n",
      "11:21:39 [DEBUG] test episode 17: reward = -72.00, steps = 73\n",
      "11:21:39 [DEBUG] test episode 18: reward = -72.00, steps = 73\n",
      "11:21:39 [DEBUG] test episode 19: reward = -86.00, steps = 87\n",
      "11:21:39 [DEBUG] test episode 20: reward = -82.00, steps = 83\n",
      "11:21:39 [DEBUG] test episode 21: reward = -177.00, steps = 178\n",
      "11:21:39 [DEBUG] test episode 22: reward = -78.00, steps = 79\n",
      "11:21:39 [DEBUG] test episode 23: reward = -207.00, steps = 208\n",
      "11:21:39 [DEBUG] test episode 24: reward = -95.00, steps = 96\n",
      "11:21:39 [DEBUG] test episode 25: reward = -80.00, steps = 81\n",
      "11:21:39 [DEBUG] test episode 26: reward = -64.00, steps = 65\n",
      "11:21:39 [DEBUG] test episode 27: reward = -73.00, steps = 74\n",
      "11:21:39 [DEBUG] test episode 28: reward = -75.00, steps = 76\n",
      "11:21:39 [DEBUG] test episode 29: reward = -143.00, steps = 144\n",
      "11:21:39 [DEBUG] test episode 30: reward = -64.00, steps = 65\n",
      "11:21:39 [DEBUG] test episode 31: reward = -64.00, steps = 65\n",
      "11:21:39 [DEBUG] test episode 32: reward = -96.00, steps = 97\n",
      "11:21:40 [DEBUG] test episode 33: reward = -80.00, steps = 81\n",
      "11:21:40 [DEBUG] test episode 34: reward = -64.00, steps = 65\n",
      "11:21:40 [DEBUG] test episode 35: reward = -93.00, steps = 94\n",
      "11:21:40 [DEBUG] test episode 36: reward = -71.00, steps = 72\n",
      "11:21:40 [DEBUG] test episode 37: reward = -71.00, steps = 72\n",
      "11:21:40 [DEBUG] test episode 38: reward = -64.00, steps = 65\n",
      "11:21:40 [DEBUG] test episode 39: reward = -71.00, steps = 72\n",
      "11:21:40 [DEBUG] test episode 40: reward = -85.00, steps = 86\n",
      "11:21:40 [DEBUG] test episode 41: reward = -71.00, steps = 72\n",
      "11:21:40 [DEBUG] test episode 42: reward = -79.00, steps = 80\n",
      "11:21:40 [DEBUG] test episode 43: reward = -64.00, steps = 65\n",
      "11:21:40 [DEBUG] test episode 44: reward = -65.00, steps = 66\n",
      "11:21:40 [DEBUG] test episode 45: reward = -96.00, steps = 97\n",
      "11:21:40 [DEBUG] test episode 46: reward = -84.00, steps = 85\n",
      "11:21:40 [DEBUG] test episode 47: reward = -78.00, steps = 79\n",
      "11:21:40 [DEBUG] test episode 48: reward = -78.00, steps = 79\n",
      "11:21:40 [DEBUG] test episode 49: reward = -91.00, steps = 92\n",
      "11:21:40 [DEBUG] test episode 50: reward = -86.00, steps = 87\n",
      "11:21:40 [DEBUG] test episode 51: reward = -80.00, steps = 81\n",
      "11:21:40 [DEBUG] test episode 52: reward = -71.00, steps = 72\n",
      "11:21:40 [DEBUG] test episode 53: reward = -87.00, steps = 88\n",
      "11:21:40 [DEBUG] test episode 54: reward = -77.00, steps = 78\n",
      "11:21:40 [DEBUG] test episode 55: reward = -141.00, steps = 142\n",
      "11:21:40 [DEBUG] test episode 56: reward = -76.00, steps = 77\n",
      "11:21:40 [DEBUG] test episode 57: reward = -77.00, steps = 78\n",
      "11:21:40 [DEBUG] test episode 58: reward = -89.00, steps = 90\n",
      "11:21:40 [DEBUG] test episode 59: reward = -93.00, steps = 94\n",
      "11:21:40 [DEBUG] test episode 60: reward = -85.00, steps = 86\n",
      "11:21:40 [DEBUG] test episode 61: reward = -93.00, steps = 94\n",
      "11:21:40 [DEBUG] test episode 62: reward = -80.00, steps = 81\n",
      "11:21:40 [DEBUG] test episode 63: reward = -169.00, steps = 170\n",
      "11:21:40 [DEBUG] test episode 64: reward = -91.00, steps = 92\n",
      "11:21:40 [DEBUG] test episode 65: reward = -65.00, steps = 66\n",
      "11:21:40 [DEBUG] test episode 66: reward = -78.00, steps = 79\n",
      "11:21:40 [DEBUG] test episode 67: reward = -80.00, steps = 81\n",
      "11:21:40 [DEBUG] test episode 68: reward = -125.00, steps = 126\n",
      "11:21:40 [DEBUG] test episode 69: reward = -85.00, steps = 86\n",
      "11:21:40 [DEBUG] test episode 70: reward = -72.00, steps = 73\n",
      "11:21:40 [DEBUG] test episode 71: reward = -70.00, steps = 71\n",
      "11:21:40 [DEBUG] test episode 72: reward = -96.00, steps = 97\n",
      "11:21:40 [DEBUG] test episode 73: reward = -100.00, steps = 101\n",
      "11:21:40 [DEBUG] test episode 74: reward = -78.00, steps = 79\n",
      "11:21:40 [DEBUG] test episode 75: reward = -72.00, steps = 73\n",
      "11:21:41 [DEBUG] test episode 76: reward = -500.00, steps = 500\n",
      "11:21:41 [DEBUG] test episode 77: reward = -177.00, steps = 178\n",
      "11:21:41 [DEBUG] test episode 78: reward = -71.00, steps = 72\n",
      "11:21:41 [DEBUG] test episode 79: reward = -76.00, steps = 77\n",
      "11:21:41 [DEBUG] test episode 80: reward = -72.00, steps = 73\n",
      "11:21:41 [DEBUG] test episode 81: reward = -85.00, steps = 86\n",
      "11:21:41 [DEBUG] test episode 82: reward = -85.00, steps = 86\n",
      "11:21:41 [DEBUG] test episode 83: reward = -97.00, steps = 98\n",
      "11:21:41 [DEBUG] test episode 84: reward = -70.00, steps = 71\n",
      "11:21:41 [DEBUG] test episode 85: reward = -94.00, steps = 95\n",
      "11:21:41 [DEBUG] test episode 86: reward = -79.00, steps = 80\n",
      "11:21:41 [DEBUG] test episode 87: reward = -199.00, steps = 200\n",
      "11:21:41 [DEBUG] test episode 88: reward = -80.00, steps = 81\n",
      "11:21:41 [DEBUG] test episode 89: reward = -71.00, steps = 72\n",
      "11:21:41 [DEBUG] test episode 90: reward = -72.00, steps = 73\n",
      "11:21:41 [DEBUG] test episode 91: reward = -98.00, steps = 99\n",
      "11:21:41 [DEBUG] test episode 92: reward = -72.00, steps = 73\n",
      "11:21:41 [DEBUG] test episode 93: reward = -84.00, steps = 85\n",
      "11:21:41 [DEBUG] test episode 94: reward = -93.00, steps = 94\n",
      "11:21:41 [DEBUG] test episode 95: reward = -91.00, steps = 92\n",
      "11:21:41 [DEBUG] test episode 96: reward = -82.00, steps = 83\n",
      "11:21:41 [DEBUG] test episode 97: reward = -90.00, steps = 91\n",
      "11:21:41 [DEBUG] test episode 98: reward = -72.00, steps = 73\n",
      "11:21:41 [DEBUG] test episode 99: reward = -213.00, steps = 214\n",
      "11:21:41 [INFO] average episode reward = -94.81 ± 51.99\n"
     ]
    }
   ],
   "source": [
    "def play_episode(env, agent, max_episode_steps=None, mode=None, render=False):\n",
    "    observation, reward, done = env.reset(), 0., False\n",
    "    agent.reset(mode=mode)\n",
    "    episode_reward, elapsed_steps = 0., 0\n",
    "    while True:\n",
    "        action = agent.step(observation, reward, done)\n",
    "        if render:\n",
    "            env.render()\n",
    "        if done:\n",
    "            break\n",
    "        observation, reward, done, _ = env.step(action)\n",
    "        episode_reward += reward\n",
    "        elapsed_steps += 1\n",
    "        if max_episode_steps and elapsed_steps >= max_episode_steps:\n",
    "            break\n",
    "    agent.close()\n",
    "    return episode_reward, elapsed_steps\n",
    "\n",
    "\n",
    "logging.info('==== test ====')\n",
    "episode_rewards = []\n",
    "for episode in range(100):\n",
    "    episode_reward, elapsed_steps = play_episode(env, agent)\n",
    "    episode_rewards.append(episode_reward)\n",
    "    logging.debug('test episode %d: reward = %.2f, steps = %d',\n",
    "            episode, episode_reward, elapsed_steps)\n",
    "logging.info('average episode reward = %.2f ± %.2f',\n",
    "        np.mean(episode_rewards), np.std(episode_rewards))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "env.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
